import api from '@/api/index.ts';
export const optionsArr = {
  //产品样式
  productStyle: [
    {
      label: '平张',
      value: 1,
    },
    {
      label: '卷式',
      value: 2,
    },
    {
      label: '折页',
      value: 3,
    },
    {
      label: '手账',
      value: 4,
    },
  ],
  //产品单位
  productUnit: [
    {
      label: '枚',
      value: '枚',
    },
    {
      label: '套',
      value: '套',
    },
    {
      label: '卷',
      value: '卷',
    },
    {
      label: '米',
      value: '米',
    },
    {
      label: '张',
      value: '张',
    },
  ],
  //出标方向
  outDirection: [
    {
      label: '上',
      value: 1,
    },
    {
      label: '下',
      value: 2,
    },
    {
      label: '左',
      value: 3,
    },
    {
      label: '右',
      value: 4,
    },
  ],
  //卷向
  scrollDirection: [
    {
      label: '未选',
      value: 0,
    },
    {
      label: '页面朝内',
      value: 1,
    },
    {
      label: '页面朝外',
      value: 2,
    },
  ],
  //贴标方式
  tagAffixWay: [
    {
      label: '未选',
      value: 0,
    },
    {
      label: '自动贴',
      value: 1,
    },
    {
      label: '手帖',
      value: 2,
    },
  ],
  //标签类型
  tagType: [
    {
      label: '刮开',
      value: 1,
    },
    {
      label: '揭开',
      value: 2,
    },
    {
      label: '刮开+揭开',
      value: 3,
    },
    {
      label: '印刷',
      value: 4,
    },
    {
      label: '空白标签',
      value: 5,
    },
  ],
  //标签形状
  tagShape: [
    {
      label: '长方形',
      value: '1',
    },
    {
      label: '正方形',
      value: '2',
    },
    {
      label: '圆形',
      value: '3',
    },
    {
      label: '椭圆形',
      value: '4',
    },
    {
      label: '异形',
      value: '5',
    },
  ],
  //生产方式
  productionMode: [
    {
      label: '版辊尺寸',
      value: 1,
    },
    {
      label: '数码版面',
      value: 2,
    },
  ],
  //彩稿参考类型
  colorType: [
    {
      label: '彩稿',
      value: 1,
    },
    {
      label: '样标',
      value: 2,
    },
    {
      label: '封样',
      value: 3,
    },
  ],
  //生产单状态
  taskProduceState: [
    {
      label: '生产中',
      value: 1,
    },
    {
      label: '待停工',
      value: 2,
    },
    {
      label: '已停工',
      value: 3,
    },
    {
      label: '已完成',
      value: 4,
    },
    {
      label: '待生产',
      value: 0,
    },
  ],
  //计价方式
  paymentMethod: [
    {
      label: '自动计算',
      value: 0,
      type: 'info',
    },
    {
      label: '按照数量',
      value: 1,
      type: '',
    },
    {
      label: '按最低价',
      value: 2,
      type: '',
    },
  ],
  //生产设备
  machine: [],
  //生产工艺
  produceCraft: [],
  //生产工艺状态
  craftTaskState: [
    {
      label: '待生产',
      value: -1,
      type: 'info',
    },
    {
      label: '待开始',
      value: 0,
      type: 'info',
    },
    {
      label: '生产中',
      value: 1,
      type: '',
    },
    {
      label: '已完成',
      value: 2,
      type: 'success',
    },
  ],
  //快递费用归属
  courierFeeType: [
    {
      label: '客户承担',
      value: 0,
      type: 'success',
    },
    {
      label: '公司承担',
      value: 1,
      type: 'info',
    },
    {
      label: '各自一半',
      value: 2,
      type: '',
    },
  ],
  //性别
  sex: [
    {
      label: '男',
      value: 1,
      type: 'primary',
    },
    {
      label: '女',
      value: 2,
      type: 'danger',
    },
  ],
  //发票类型
  invoiceType: [
    {
      label: '普通发票',
      value: 1,
      type: 'primary',
    },
    {
      label: '增值税专用发票',
      value: 2,
      type: 'danger',
    },
  ],
  //产品来源
  productSource: [
    {
      label: '新系统',
      value: 0,
    },
    {
      label: '老系统',
      value: 1,
    },
  ],
  //产品版本
  productLayout: [
    {
      label: '新版',
      value: '新版',
      type: 'success',
    },
    {
      label: '改版',
      value: '改版',
      type: 'danger',
    },
  ],
  //数码颜色
  technoColor: [
    {
      label: '黑码',
      value: 0,
    },
    {
      label: '彩码',
      value: 1,
    },
  ],
  goodGroup: [], //物料分组
  surfaceRequires: [], //表面要求
  supplier: [], //供应商
  inStockType: [], //入库类型
  outStockType: [], //出库类型
  goods: [], //所有物料
  goodType: [], //物料类型
  product: [], //所有产品
  client: [], //所有客户
  organization: [], //组织结构
  roles: [], //角色权限
  department: [], //部门
  employeeType: [], //员工类型
  employeeState: [], //员工状态
  nation: [], //民族
  residenceType: [], //户籍类型
  politicsStatus: [], //政治面貌
  maritalStatus: [], //婚姻状况
  qualification: [], //学历
  personnel: [], //员工
  AreaManagement: [], //所有地址
  informationType: [], //资料类型
  customer: [], //所有员工
  productType: [], //产品类型
  productColor: [], //产品颜色
  paperBox: [], //纸箱
  supplierType: [], //供应商类型
  settlementWay: [], //供应商结算方式
  device: [], //设备
  warehouse: [], //仓库
};
//递归重置tree
const transformTree = (data: any, labelName: string, valeuName: string, childrenName: string = 'children') => {
  // 如果是叶子节点且 children 为 null，则删除 children 属性
  if (!data[childrenName] || data[childrenName] === null) {
    return {
      ...data,
      label: data[labelName],
      value: data[valeuName],
    };
  }

  // 否则递归处理 children
  const transformed = {
    label: data[labelName],
    value: data[valeuName],
    ...data,
    children: data[childrenName].map((child: any) => transformTree(child, labelName, valeuName, childrenName)),
  };

  return transformed;
};
//列表-获取仓库
export const getWarehouse = (
  options: any = optionsArr, //
  optionsName: string = 'warehouse', //
  data: any = {
    likeWords: '',
  },
) => {
  if (options[optionsName].length > 0) return;
  api.Business.GetStockAreaTreeView(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...transformTree(el, 'name', 'code'),
    }));
  });
};
//列表-供应商结算方式
export const getSettlementWay = (
  options: any = optionsArr, //
  optionsName: string = 'settlementWay', //
  data: any = {},
) => {
  api.Public.GetSettlementWayDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-供应商类型
export const getSupplierType = (
  options: any = optionsArr, //
  optionsName: string = 'supplierType', //
  data: any = {},
) => {
  api.Public.GetSupplierTypeDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取纸箱规格
export const getPaperBox = (
  options: any = optionsArr, //
  optionsName: string = 'paperBox', //
  fun?: Function,
  data: any = {
    queryDataBaseFiled: '',
    likeWords: '',
    pageIndex: 1,
    pageSize: 9999,
    onlySelf: 0,
    isShowDisabled: -1,
    orderByFiled: '',
    orderByType: 0,
  },
) => {
  api.System.GetPageCartonConfigurationList(data).then((res: any) => {
    options[optionsName] = res.list.map((el: any) => ({
      ...el,
      label: `${el.cartonName}【${el.cartonWeight}kg】`,
      value: el.cartonCode,
    }));
    if (fun && typeof fun === 'function') {
      fun(options[optionsName][0] || {});
    }
  });
};
//列表-获取产品颜色
export const getProductColor = (
  options: any = optionsArr, //
  optionsName: string = 'productColor', //
  fun?: Function,
  data: any = {
    useType: 2,
    colorType: 0,
    likeWords: '',
  },
) => {
  api.System.QueryPrintingColorDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.colorName,
      value: el.colorCode,
    }));
    if (fun && typeof fun === 'function') {
      fun(options[optionsName][0] || {});
    }
  });
};
//列表-获取产品类型
export const getProductType = (
  options: any = optionsArr, //
  optionsName: string = 'productType', //
  fun?: Function,
  data: any = {},
) => {
  api.Public.GetSaleOrderTypeDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.objectName,
      value: el.objectCode,
    }));
    if (typeof fun === 'function' && res?.length > 0) {
      fun(res[0]);
    }
  });
};
//列表-获取所有客户
export const getCustomer = (
  options: any = optionsArr, //
  optionsName: string = 'customer', //
  data: any = {
    pageIndex: 1, //当前页码
    pageSize: 99999, //每页显示数据的数量
    queryDataBaseFiled: '', // 关键字查询字段，空值查询全部
    likeWords: '', // 查询关键字，空值查询全部
    beginTime: '', // 日期范围-开始时间
    endTime: '', // 日期范围-结束时间
  },
) => {
  api.OrderBusniess.QueryPageCustomerList(data).then((res: any) => {
    options[optionsName] = res.list.map((el: any) => ({
      ...el,
      label: el.customerName,
      value: el.customerCode,
    }));
  });
};
//列表-获取资料类型
export const getInformationType = (
  options: any = optionsArr, //
  optionsName: string = 'informationType', //
  data: any = {},
) => {
  api.OrderBusniess.QueryInformationTypeDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.objectName,
      value: el.objectCode,
    }));
  });
};
//列表-获取地址信息
export const getTreeAreaDataList = (
  options: any = optionsArr, //
  optionsName: string = 'AreaManagement', //
  data: any = {},
) => {
  if (options[optionsName].length > 0) return;
  api.AreaManagement.GetTreeAreaDataList(data).then((res: any) => {
    options[optionsName] = res;
  });
};
//列表-获取所有员工
export const getPersonnel = (
  options: any = optionsArr, //
  optionsName: string = 'personnel', //
  data: any = {
    likeWords: '',
    organizationCode: '',
    departmentCode: '',
  },
) => {
  api.Business.QueryMatchEmployeeDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.employeeName,
      value: el.employeeCode,
    }));
  });
};
//列表-获取学历
export const getQualification = (
  options: any = optionsArr, //
  optionsName: string = 'qualification', //
  data: any = {},
) => {
  api.Business.GetDegreeList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取婚姻状况
export const getMaritalStatus = (
  options: any = optionsArr, //
  optionsName: string = 'maritalStatus', //
  data: any = {},
) => {
  api.Business.GetEmployeeMaritalStatusList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取户籍类型
export const getPoliticsStatus = (
  options: any = optionsArr, //
  optionsName: string = 'politicsStatus', //
  data: any = {},
) => {
  api.Business.GetPoliticsStatusList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取户籍类型
export const getResidenceType = (
  options: any = optionsArr, //
  optionsName: string = 'residenceType', //
  data: any = {},
) => {
  api.Business.GetRegisteredResidenceTypeList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取民族
export const getNation = (
  options: any = optionsArr, //
  optionsName: string = 'nation', //
  data: any = {},
) => {
  api.Business.GetEmployeeNationList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取员工状态
export const getEmployeeState = (
  options: any = optionsArr, //
  optionsName: string = 'employeeState', //
  data: any = {},
) => {
  api.Business.GetEmployeeStateList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取员工类型
export const getEmployeeType = (
  options: any = optionsArr, //
  optionsName: string = 'employeeType', //
  data: any = {},
) => {
  api.Business.GetEmployeeTypeList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取所有角色权限
export const getRoles = (
  options: any = optionsArr, //
  optionsName: string = 'roles', //
  data: any = {},
) => {
  api.SystemManage.QueryMatchSystemRoleDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.roleName,
      value: el.roleCode,
    }));
  });
};
//列表-获取所有产品
export const getProduct = (
  options: any = optionsArr,
  optionsName: string = 'product',
  data: any = {
    queryDataBaseFiled: '', // 关键字查询字段，空值查询全部
    likeWords: '', // 查询关键字，空值查询全部
    isShowDisabled: 1, // 是否显示禁用,0：显示；1不显示
    customerCode: '', // 排序字段
  },
) => {
  api.OrderBusniess.GetProductBriefList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.productName,
      value: el.productCode,
    }));
  });
};
//列表-获取所有客户
export const getClient = (
  options: any = optionsArr,
  optionsName: string = 'client',
  data: any = {
    pageIndex: 1, // 当前页码 (从1开始)
    pageSize: 999999, // 每页显示数据的数量
    queryDataBaseFiled: '', // 关键字查询字段，空值查询全部
    likeWords: '', // 查询关键字，空值查询全部
    beginTime: '', // 日期范围-开始时间
    endTime: '', // 日期范围-结束时间
    onlySelf: 0, // 是否只查询自己创建的，0：查所有、1：只查自己创建的
    isShowDisabled: 0, // 是否显示禁用,-1：显示所有的；0：仅显示未作废的；1 仅显示作废的
    orderByFiled: '', // 排序字段
    orderByType: 0, // 排序方式，0-倒序,1-升序
    settlementWay: 0, // 结算方式：1日结；2月结，0查询全部
    belongToSalesman: '', // 销售代表代码，空值查询全部
    isEnabled: 1, // 是否启用：-1：查全部；1只查启用的；0只查禁用的
  },
) => {
  api.OrderBusniess.QueryPageCustomerList(data).then((res: any) => {
    options[optionsName] = res.list.map((el: any) => ({
      ...el,
      label: el.customerName,
      value: el.customerCode,
    }));
  });
};
//列表-获取物料类型
export const getGoodType = (
  options: any = optionsArr,
  optionsName: string = 'goodType',
  data: any = {
    DictionaryCode: 'goodsCategory', // 物料分组代码，空值查询全部
  },
) => {
  api.Business.GetDictionaryByCode(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.name,
      value: el.code,
    }));
  });
};
//列表-获取基础数据
export const getDictionary = (
  options: any = optionsArr,
  optionsName: string = 'goodType',
  data: any = {
    DictionaryCode: 'ReceivingType',
  },
) => {
  if (options[optionsName]?.length > 0) return;
  api.Business.GetDictionaryByCode(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.name,
      value: el.code,
    }));
  });
};
//列表-获取物料
export const getGoods = (
  options: any = optionsArr,
  optionsName: string = 'goods',
  data: any = {
    groupCode: '', // 物料分组代码，空值查询全部
    categoryCode: '', // 物料类型代码，空值查询全部
    likeWords: '', // 查询关键字，空值查询全部
  },
) => {
  api.Warehouse.GetGoodsDataList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.goodsName,
      value: el.goodsCode,
    }));
  });
};
//列表-获取出库类型
export const getOutStockType = (
  options: any = optionsArr, //
  optionsName: string = 'outStockType', //
) => {
  api.System.GetGoodsOutStockTypeDataList().then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.objectName,
      value: el.objectCode,
    }));
  });
};
//列表-获取入库类型
export const getInStockType = (
  options: any = optionsArr, //
  optionsName: string = 'inStockType', //
) => {
  api.System.GetGoodsInStockTypeDataList().then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.objectName,
      value: el.objectCode,
    }));
  });
};
//列表-获取供应商
export const getSupplier = (
  options: any = optionsArr, //
  optionsName: string = 'supplier', //
  data: any = {
    pageIndex: 1,
    pageSize: 999999,
    queryDataBaseFiled: '', // 关键字查询字段，空值查询全部
    likeWords: '', // 查询关键字，空值查询全部
    beginTime: '', // 日期范围-开始时间
    endTime: '', // 日期范围-结束时间
    onlySelf: 0, // 是否只查询自己创建的，0：查所有、1：只查自己创建的
    isShowDisabled: 0, // 是否显示禁用,-1：显示所有的；0：仅显示未作废的；1 仅显示作废的
    orderByFiled: '', // 排序字段
    orderByType: 0, // 排序方式，0-倒序,1-升序
    supplierType: '', // 供应商类型，空值查询全部
    settlementWay: '', // 结算方式，空值查询全部
    supplierState: 0, // 供应商状态：0可用；1已禁用，-1查询全部
  },
) => {
  if (options[optionsName].length > 0) {
    return;
  }
  api.Supplier.GetMatchPageSuppliers(data).then((res: any) => {
    options[optionsName] = res.list.map((el: any) => ({
      ...el,
      label: el.supplierName,
      value: el.supplierCode,
    }));
  });
};
//列表-获取表面要求
export const getSurfaceRequires = (
  options: any = optionsArr, //
  optionsName: string = 'surfaceRequires', //
  data: any = {
    pageIndex: 1,
    pageSize: 999999,
    queryDataBaseFiled: '',
    likeWords: '',
    isShowDisabled: 1,
    dictionaryCode: 'SurfaceRequire',
    dicValueState: -1,
    isDelete: -1,
  },
) => {
  api.System.QueryPageDictionaryValueDataList(data).then((res: any) => {
    options[optionsName] = res.list.map((el: any) => ({
      ...el,
      label: el.displayValue,
      value: el.dicValueCode,
    }));
  });
};
//列表-获取物料分组
export const goodGroup = (
  options: any = optionsArr, //
  optionsName: string = 'goodGroup', //
  fun?: Function,
) => {
  api.GoodsGroup.GetAllGoodsGroupData().then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.groupName,
      value: el.groupCode,
    }));
    _sun();
  });
  function _sun() {
    if (fun && typeof fun === 'function') {
      fun(options[optionsName]);
    }
  }
};
//列表-获取设备
export const getDevice = (
  options: any = optionsArr, //
  optionsName: string = 'device', //
  data: any = {
    equipmentState: 1, // 机台状态：1可用；0禁用，-1查询全部
    likeWords: '', // 查询关键字，空值查询全部
    machineCode: '', // 设备code
  },
) => {
  api.ProduceMachine.GetProduceMachinesEquipmentsByMachineCode(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.equipmentName,
      value: el.equipmentCode,
    }));
  });
};
//列表-获取机台
export const getMachine = (
  options: any = optionsArr, //
  optionsName: string = 'machine', //
  data: any = {
    machineType: '', // 机台类型代码，空值查询全部
    ownDepartment: 0, // 归属部门：1生产部；2检验部，0查询全部
    machineState: 1, // 机台状态：1可用；0禁用，-1查询全部
    likeWords: '', // 查询关键字，空值查询全部
  },
) => {
  api.ProduceMachine.GetMatchProduceMachineList(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.machineName,
      value: el.machineCode,
    }));
  });
};
//列表-获取工艺数据
export const getProduceCraft = (
  options: any = optionsArr, //
  optionsName: string = 'produceCraft', //
  data: any = {
    likeWords: '', // 工艺名称
  },
) => {
  api.ProductCraft.GetMatchProductCrafts(data).then((res: any) => {
    options[optionsName] = res.map((el: any) => ({
      ...el,
      label: el.name,
      value: el.code,
    }));
  });
};
//列表-获取组织结构
export const getOrganization = (
  options: any = optionsArr, //
  optionsName: string = 'organization', //
  data: any = {},
) => {
  api.Organization.GetAllOrganizations(data).then((res: any) => {
    api.Department.GetDepartmentTreeData({
      orgCode: res[0].orgCode, //组织唯一识别码
    }).then((res: any) => {
      options[optionsName] = res.map((el: any) => ({
        ...el,
        label: el.departmentName,
        value: el.departmentCode,
      }));
    });
  });
};
